package com.aiyige.page.login.model.impl;

import android.content.Context;
import android.text.TextUtils;

import com.aiyige.base.api.ApiManager;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.SmsCode;
import com.aiyige.model.response.ErrorResponse;
import com.aiyige.page.login.ResetPasswordPage;
import com.aiyige.page.login.callbacks.ISendVerifyCodeCallBack;
import com.aiyige.page.login.model.IFindPasswordModel;
import com.aiyige.page.login.view.IFindpasswordView;
import com.aiyige.utils.ToastX;
import com.aiyige.utils.VerifyCodeManager;
import com.vondear.rxtools.RxTool;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 注册实现
 */
public class FindPasswordImplement implements IFindPasswordModel {
    public static final String TAG = FindPasswordImplement.class.getSimpleName();
    private static FindPasswordImplement mInstance;

    IFindpasswordView mView;

    public static synchronized FindPasswordImplement getInstance() {
        if (mInstance == null) {
            mInstance = new FindPasswordImplement();
        }
        return mInstance;
    }

    private SmsCode smsCode;

    boolean isSending = false;

    @Override
    public void checkVerifyCode(final String mobile, String code) {
        if (smsCode != null && !TextUtils.isEmpty(mobile) && !TextUtils.isEmpty(code) && smsCode.getMobile().equals(mobile) && RxTool.Md5(code).equals(smsCode.getCode())) {
            ResetPasswordPage.start(mobile, code);
        } else {
            ToastX.show("手机号或验证码不正确");
        }
    }

    @Override
    public void sendVerifyCode(Context context, final String phone, final ISendVerifyCodeCallBack codeCallBack) {
        if (isSending) {
            return;
        }
        VerifyCodeManager.getInstance().tCaptchaDialog(context, new VerifyCodeManager.TCaptchaCallBack() {
            @Override
            public void success() {
                isSending = true;
                ApiManager.getService().fetchByMobile(phone, "password").enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(final Call<ResponseBody> call, final Response<ResponseBody> response) {
                        isSending = false;
                        try {
                            if (response.isSuccessful()) {
                                smsCode = JsonUtil.toObject(response.body().string(), SmsCode.class);
                                VerifyCodeManager.getInstance().start();
                            } else {
                                ErrorResponse data = JsonUtil.toObject(response.errorBody().string(), ErrorResponse.class);
                                onFailure(call,new Throwable(data.getMessage()));
                            }
                        } catch (Exception e) {
                            try {
                                onFailure(call,new Throwable(e.toString()));
                            } catch (Exception e1) {
                                e1.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        isSending = false;
                        if (t != null) {
                            ToastX.show(t.getMessage());
                        }
                    }
                });
            }

            @Override
            public void failed() {

            }
        });



    }

    @Override
    public void setView(IFindpasswordView view) {
        this.mView = view;
    }


}
